package com.ai.cppdemo

import android.content.Context
import android.media.MediaCodec
import android.media.MediaFormat
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.ai.cppdemo.databinding.ActivityMainBinding
import java.nio.Buffer
import java.nio.ByteBuffer

private const val TAG = "MainActivity"

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        getSystemService(Context.MEDIA_PROJECTION_SERVICE)


        // Example of a call to a native method
        binding.sampleText.text = stringFromJNI()

        binding.plus.setOnClickListener {
            Toast.makeText(this, "${sum(1, 2)}", Toast.LENGTH_SHORT).show()

        }

        binding.minus.setOnClickListener {
            Toast.makeText(this, "${minus(1, 2)}", Toast.LENGTH_SHORT).show()
            sum(3, 4)
        }

        binding.operateArray.setOnClickListener {
            val elements = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
            Toast.makeText(
                this, "${accumulate(elements)}", Toast.LENGTH_SHORT
            ).show()
            for (element in elements) {
                Log.d(TAG, "element=${element}")
            }
        }

        binding.registerExit.setOnClickListener {
            registerExitFun()
        }

        binding.pltHook.setOnClickListener {
           pltHook()
        }
    }

    /**
     * A native method that is implemented by the 'cppdemo' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String
    external fun sum(a: Int, b: Int): Int
    external fun minus(a: Int, b: Int): Int
    external fun registerExitFun()
    external fun accumulate(elements: IntArray): Int
    external fun pltHook()
    external fun killProcess()

    companion object {
        // Used to load the 'cppdemo' library on application startup.
        init {
            System.loadLibrary("cppdemo")
        }
    }
}